<%@ page language="java"  pageEncoding="UTF-8" errorPage="../error.jsp"%>
<%-- <%@ taglib prefix="s" uri="/struts-tags" %> --%>
<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core'%>
<%
	pageContext.setAttribute("ctx", request.getContextPath());	
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<link rel="stylesheet"  type="text/css" href="${ctx}/css/default.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/js/jqueryeasyui12/themes/bootstrap/easyui.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/js/jqueryeasyui12/themes/icon.css" />
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="${ctx}/js/jqueryeasyui12/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${ctx}/js/jqueryeasyui12/locale/easyui-lang-zh_TW.js"></script>
<script type="text/javascript" src="${ctx}/js/ipebg/IPEBG.js"></script>
<script type="text/javascript" src="${ctx}/js/ipebg/COMMON.js"></script>
<script type="text/javascript">
    var ctx = '${ctx}';
    jQuery(document).ready(function(){
        $("table[id=tableShow]").show();
		$('#qryGrid').datagrid({
			title:'用户列表',
			nowrap: false,
			striped: true,
			fitColumns: true,
			url: ctx + '/user.do?action=list',
			remoteSort: false,
			//idField:'typeCode',
			singleSelect:true,
			rownumbers:true,
			frozenColumns:[[
			    {field:'ck',checkbox:true,width:30},
				{field:'userId',title:'Id',width:50,hidden: true},
				{field:'orgId',title:'orgId',width:100,hidden: true}
			]],
			columns:[[	
				{field:'loginName',title:'工號',width:100},
				{field:'realName',title:'姓名',width:100},
				{field:'orgName',title:'部門',width:100},
				{field:'dominate',title:'廠區',width:100,
				    formatter:function(value,rowdata,index) {
				    	if(value=='ZZK') {
				    		return "鄭州綜保區";
				    	}else if(value=='ZZC'){
				    		return "鄭州加工區";
				    	}else if(value=='JYX'){
				    		return "濟源廠區";
				    	}else if(value=='JCX'){
				    		return "晉城廠區";
				    	}else if(value=='LHX'){
				    		return "龍華廠區";
				    	}else if(value=='GLX'){
				    		return "觀瀾廠區";
				    	}else if(value=='HBX'){
				    		return "鶴壁廠區";
				    	}else if(value=='NH'){
							return "內湖廠區";
						}
				    }},
				{field:'phone',title:'電話',width:100},
				{field:'telePhone',title:'分機',width:100},
				{field:'email',title:'Email地址',width:200},
				{field:'enabled',title:'狀態',width:50,
				   	formatter:function(value,rowdata,index) {
				   		if(value=='0') {
				   			return "<font color='red'>不可用</font>";
				   		}else if(value=='1') {
				   			return "可用";
				   		}
				   	}},
				{field:'createdTime',title:'創建時間',width:100}	
			]],
			pagination:true,
			pageSize:10,
			pageNumber:1,
			pageList:[10,20,30]
		});
		var p = $('#qryGrid').datagrid('getPager');
		if (p){
			$(p).pagination({
				onBeforeRefresh:function(){
					;
				}
			});
		}
		
		$('#chooseParent').click(function() {
	    	var optionWinParent = $("#optionWinParent");
			optionWinParent.find("input").val("");
			optionWinParent.find("select").val("");
			if (optionWinParent.is(':visible')){
				;
			}else{
				optionWinParent.slideDown();//显示 
			} 
			optionWinParent.window('open');
			
	    	var params={
	    			"f_enable" : "1"
	    	};
			$('#qryOrgParentGrid').datagrid({
				//title:'組織代碼信息',
				nowrap: true,
				onLoadSuccess: function (data) {
	                $(this).datagrid('doCellTip', { 'max-width': '700px', 'delay': 500 });
	            },
				striped: true,
				fitColumns: false,
				url: ctx+'/org.do?action=orgList',
				remoteSort: false,
			    queryParams:params,
				idField:'orgId',
				singleSelect:true,
				rownumbers:true,
				frozenColumns:[[
	                {field:'ck',checkbox:true,width:30},
	                {title:'orgId',field:'orgId',width:30,hidden:true},
	                {title:'levelId',field:'levelId',width:30,hidden:true},
	                {field:'parentId',title:'parentId',width:120,hidden:true},
	                {field:'dominate',title:'廠區',width:100},
	                {field:'orgCode',title:'組織代碼',width:150},
				    {field:'orgName',title:'組織名稱',width:100}
				]],
				columns:[[
				    {field:'orgFee',title:'費用代碼',width:120},
				    {field:'levelName',title:'組織層級',width:120},
				    {field:'leaderNo',title:'主管工號',width:120},
				    {field:'leaderName',title:'主管姓名',width:120},
				    {field:'enable',title:'是否有效',width:120,
				    	formatter:function(value,rowData,rowIndex) {
							if(rowData.enable=='1') {
								return "有效";
							} else {
								return "無效";
							}
				    	}		
				    },
				    {field:'parentCode',title:'上級組織代碼',width:120},
				    {field:'parentName',title:'上級組織名稱',width:120},
				    {field:'createName',title:'創建人',width:100},
				    {field:'createTime',title:'創建時間',width:100}
				]],
				pagination:true,
				pageSize:10,
				pageNumber:1,
				pageList:[10,20,30]
			});
			
			$('#qryOrgParentGrid').datagrid('unselectAll');
	    });
	    $('#submitOrgParent').click(function() {
	    	var selected = $('#qryOrgParentGrid').datagrid('getSelected');
	    	if(selected){ 
	    		$("#orgId").val(selected.orgId);
		    	$("#orgName").val(selected.orgName);
		    	$("#optionWinParent").window('close');
	    	} else {
	    		$.messager.alert('系統提示','請先選擇一個組織!','warning');
	    	}
	    });
	    $('#cancelOrgParent').click(function() {
			$("#optionWinParent").window('close');
		});
	    
		$('#submit').click(function() {
			
			var password = $("#password").val();
			var repassword = $("#repassword").val();
			if(password != repassword){
				$.messager.alert('系統提示','密碼和重複密碼不一致，請重新輸入!','warning');
				return false;
			}
			
			//保存和修改----------------------------
			var validationResult = $("#addForm").form('validate');
	       	if(validationResult==false){
	       		$.messager.alert('系統提示','請輸入必填信息!','warning');
	       		return;
	       	}
		    $.messager.confirm('系統提示', '您確定需要提交嗎?', function(r) {
	            if (r) {	
	              	var method = $.trim($("#method").val());
	               	if(method=='add'){	          	
	               		$.post(ctx +'/user.do?action=add',$("#addForm").serializeArray(),function(data){
	                    	if(data == "success"){
	                    		$("#optionWin").window('close');
	     	                    $.messager.alert('系統提示','新增成功!','info');
	                    		$('#qryGrid').datagrid('reload');
	                    	}else if(data == "exsit"){
	                    		$.messager.alert('系統提示','料號已存在，請重新輸入!','info');
	                    	}else{
	                    		$.messager.alert('系統提示','新增失敗!','info');
	                    	}
	                    });
	               	} else if(method=='edit'){
	               		$.post(ctx +'/user.do?action=edit',$("#addForm").serializeArray(),function(data){
	                    	if(data == "success"){
	                    		$("#optionWin").window('close');
	     	                    $.messager.alert('系統提示','修改成功!','info');
	                    		$('#qryGrid').datagrid('reload');
	                    	}else if(data == "exsit"){
	                    		$.messager.alert('系統提示','料號已存在，請重新輸入!','info');
	                    	}else{
	                    		$.messager.alert('系統提示','新增失敗!','info');
	                    	}
	                    });
	               }
	     	    }
			});
		    
		});
		//取消保存-------------------------
		$('#cancel').click(function() {
	    	$('#method').val('');
	   	    $("#optionWin").window('close');
	    });
		
		//重置密碼提交
		$('#submitPwd').click(function() {
			
			var password = $("#pwd_password").val();
			var repassword = $("#pwd_repassword").val();
			if(password != repassword){
				$.messager.alert('系統提示','密碼和重複密碼不一致，請重新輸入!','warning');
				return false;
			}
			
			//保存和修改----------------------------
			var validationResult = $("#pwdForm").form('validate');
	       	if(validationResult==false){
	       		$.messager.alert('系統提示','請輸入必填信息!','warning');
	       		return;
	       	}
		    $.messager.confirm('系統提示', '您確定要重置密碼嗎?', function(r) {
	            if (r) {	
	               	$.post(ctx +'/user.do?action=reset',$("#pwdForm").serializeArray(),function(data){
	                   	if(data == "success"){
	                   		$("#optionWinModifyPwd").window('close');
	     	                $.messager.alert('系統提示','密碼重置成功!','info');
	                   		$('#qryGrid').datagrid('reload');
	                   	}else{
	                   		$.messager.alert('系統提示','密碼重置失敗!','info');
	                   	}
	                });
	     	    }
			});
		    
		});
		//取消保存-------------------------
		$('#cancelPwd').click(function() {
	   	    $("#optionWinModifyPwd").window('close');
	    });
	});
	//打開增加窗口 	        
	function addRowOpen(){
		var optionWin = $("#optionWin");
		if (optionWin.is(':visible')){
			;
		}else{
			optionWin.slideDown();//显示 
		}
		optionWin.find("input").val("");
		optionWin.find("textarea").val("");
		optionWin.find("#password").attr("readonly",false);
		optionWin.find("#repassword").attr("readonly",false);
		$("#method").val('add');
		optionWin.window('open');
	}
	//打開編輯方法
	function editRowOpen(){
		var selected = $('#qryGrid').datagrid('getSelected');
		if(selected){
			var optionWin = $("#optionWin");
			if (optionWin.is(':visible')){
			    ;
			}else{
				optionWin.slideDown();//显示 
			}					   
			optionWin.find("#userId").val(selected.userId);
			optionWin.find("#jobNo").val(selected.loginName);
			optionWin.find("#realName").val(selected.realName);
			optionWin.find("#password").val(selected.password);
			optionWin.find("#password").attr("readonly",true);
			optionWin.find("#repassword").val(selected.password);
			optionWin.find("#repassword").attr("readonly",true);
			optionWin.find("#phone").val(selected.phone);
			optionWin.find("#telePhone").val(selected.telePhone);
			optionWin.find("#orgId").val(selected.orgId);
			optionWin.find("#orgName").val(selected.orgName);
			optionWin.find("#dominate").combobox('setValue', selected.dominate);
			optionWin.find("#email").val(selected.email);
			optionWin.find("#description").val(selected.description);
			$("#method").val('edit');
			optionWin.window('open');
		}else{
			$.messager.alert('系統提示','請先選擇一筆資料再進行修改!','info');
		}
	}
	
	//刪除一行數據方法
	function deleteRow(){
		var selected = $('#qryGrid').datagrid('getSelected');
		if(selected){
			var index = $('#qryGrid').datagrid('getRowIndex', selected);
			var userId = selected.userId;				
			$.messager.confirm('系統提示', '您確定要刪除該記錄嗎?', function(r) {
	        	 if (r) {
	            	var params = {
						"userId" : userId
					};
					var actionUrl = ctx + '/user.do?action=delete';							
					$.ajax({
						type: "post",
						url : actionUrl,
						data : params,					
						dataType : "text",
						cache : false,
						error : function(textStatus, errorThrown) {
							$.messager.alert('系統提示','系統錯誤(' + textStatus + '): '+errorThrown,'error');
						},
						success : function(data, textStatus) {															
							if(data == 'success'){
								$.messager.alert('系統提示','刪除成功！','info');
								$('#qryGrid').datagrid('deleteRow', index);				
							}else if(data == "exsit"){
								$.messager.alert('系統提示','該用戶已經分配角色，不能刪除！','info');
							}else{
								$.messager.alert('系統提示','刪除失敗！','info');
							}
						}
					});
	        	}
	        });
		}else{
			$.messager.alert('系統提示','請先選擇一筆資料再進行刪除!','info');
		}
	}
	//查詢方法
	function query(){
		var loginName = $("#s_jobNo").val();
		var realName = $("#s_realName").val();
		var param = {
				"loginName" : loginName,
				"realName" : realName
			};	
		$('#qryGrid').datagrid('load',param);	
	}
	//啟用用戶方法
	function enableRow(){
		var selected = $('#qryGrid').datagrid('getSelected');
		if(selected){
			var userId = selected.userId;
			var enabled = selected.enabled;
			if(enabled == '1'){
				$.messager.alert('系統提示','該用戶已經是可用狀態，不可再次啟用!','info');
			}else{
				$.messager.confirm('系統提示', '您確定要啟用該用戶嗎?', function(r) {
		        	 if (r) {
		            	var params = {
		            		"userId" : userId,
							"enable" : "1"
						};
						var actionUrl = ctx + '/user.do?action=enable';							
						$.ajax({
							type: "post",
							url : actionUrl,
							data : params,					
							dataType : "text",
							cache : false,
							error : function(textStatus, errorThrown) {
								$.messager.alert('系統提示','系統錯誤(' + textStatus + '): '+errorThrown,'error');
							},
							success : function(data, textStatus) {															
								if(data == 'success'){
									$.messager.alert('系統提示','啟用成功！','info');
									$('#qryGrid').datagrid('reload');		
								}else{
									$.messager.alert('系統提示','啟用失敗！','info');
								}
							}
						});
		        	}
		        });
			}
		}else{
			$.messager.alert('系統提示','請先選擇一筆資料再進行啟用!','info');
		}
	}
	//禁用用戶方法
	function disableRow(){
		var selected = $('#qryGrid').datagrid('getSelected');
		if(selected){
			var userId = selected.userId;
			var enabled = selected.enabled;
			if(enabled == '0'){
				$.messager.alert('系統提示','該用戶已經是不可用狀態，不可再次禁用!','info');
			}else{
				$.messager.confirm('系統提示', '您確定要禁用該用戶嗎?', function(r) {
		        	 if (r) {
		            	var params = {
		            		"userId" : userId,
							"enable" : "0"
						};
						var actionUrl = ctx + '/user.do?action=enable';							
						$.ajax({
							type: "post",
							url : actionUrl,
							data : params,					
							dataType : "text",
							cache : false,
							error : function(textStatus, errorThrown) {
								$.messager.alert('系統提示','系統錯誤(' + textStatus + '): '+errorThrown,'error');
							},
							success : function(data, textStatus) {															
								if(data == 'success'){
									$.messager.alert('系統提示','禁用成功！','info');
									$('#qryGrid').datagrid('reload');		
								}else{
									$.messager.alert('系統提示','禁用失敗！','info');
								}
							}
						});
		        	}
		        });
			}
		}else{
			$.messager.alert('系統提示','請先選擇一筆資料再進行啟用!','info');
		}
	}
	
	//重置密碼方法
	function modifyPwdOpen(){
		var selected = $('#qryGrid').datagrid('getSelected');
		if(selected){
			var optionWin = $("#optionWinModifyPwd");
			if (optionWin.is(':visible')){
			    ;
			}else{
				optionWin.slideDown();//显示 
			}					   
			optionWin.find("#pwd_userId").val(selected.userId);
			optionWin.window('open');
		}else{
			$.messager.alert('系統提示','請先選擇一筆資料再進行重置密碼!','info');
		}
	}
</script>
</head> 
<body>    
    <div style="width:99%">
    <table id="qryGrid" toolbar="#tb"></table>
	<div id="tb">
		<form action="" id="tbFormId" style="margin:0px;margin-bottom:10px;">
		<table width="100%" id="tableShow" style="display:none;">
			<tr>
				<td width="60%" style="white-space:nowrap;">
					<a href="#" id="btnadd" class="easyui-linkbutton" iconCls="icon-add" plain="true" code="ADD" onclick="addRowOpen()">新增</a>
					<a href="#" id="btnedit" class="easyui-linkbutton" iconCls="icon-edit" plain="true" code="EDIT" onclick="editRowOpen()">修改</a>
					<a href="#" id="btncut" class="easyui-linkbutton" iconCls="icon-remove" plain="true" code="DELETE" onclick="deleteRow()">刪除</a>
					<a href="#" id="btncut" class="easyui-linkbutton" iconCls="icon-ok" plain="true" code="ENABLE" onclick="enableRow()">啟用</a>
					<a href="#" id="btncut" class="easyui-linkbutton" iconCls="icon-no" plain="true" code="DISABLE" onclick="disableRow()">禁用</a>
					<a href="#" id="btnedit" class="easyui-linkbutton" iconCls="icon-edit" plain="true" code="RESETPASSWORD"  onclick="modifyPwdOpen()">重置密碼</a>
				</td>
				<td width="40%" style="white-space:nowrap;padding:0px" align="right">
					<span style="font-size:13px;">工號:</span>
					<input type="text" id="s_jobNo" />
									
					<span style="font-size:13px;">姓名:</span>
					<input type="text" id="s_realName" />
					<a class="easyui-linkbutton" iconCls="icon-search" plain="true" href="#" code="QUERY" onclick="query()">查詢</a>
				</td>
			</tr>
		</table>
		</form>
	</div>
	</div>
	
	<div id="optionWin" class="easyui-window" title="用戶信息維護" closed="true" modal="true" 
	     collapsible="false" minimizable="false" maximizable="false" draggable="true" resizable="false"
	     style="width:650px;height:400px;padding:5px;left:60px;top:50px;background: #fafafa;display:none" align="center">		
		<form id="addForm" name="addForm" action="" method="post">
			<input type="hidden" id="userId" name="userId"/>
			<input type="hidden" id="method"/>
        	<table class="formTbl" style="font-size:13px">
        	 	<tr>
					<th align="right" style="width:65px">工號：</th>
					<td>
					    <input type="text" id="jobNo" name="loginName" class="easyui-validatebox" style="width:200px;" required="true" >
					    <font color="red">*</font>
					</td>
					<th align="right" style="width:65px">姓名：</th>
					<td>
					    <input id="realName" type="text" name="realName" style="width:200px;" class="easyui-validatebox" required="true" >				 
					    <font color="red">*</font>
					</td>
				</tr>
				<tr>
					<th align="right">密碼：</th>
					<td>
					    <input id="password" type="password" name="password" style="width:200px;" class="easyui-validatebox" required="true">
					    <font color="red">*</font>
					</td>
					<th align="right">確認密碼：</th>
					<td>
					    <input id="repassword" type="password" style="width:200px;" class="easyui-validatebox"  required="true" >				 
					    <font color="red">*</font>
					</td>
				</tr>
				<tr>
					<th align="right">电话：</th>
					<td>
					    <input id="phone" type="text" name="phone" style="width:200px;" class="easyui-validatebox" required="true">
					    <font color="red">*</font>
					</td>
					<th align="right">分机：</th>
					<td>
					    <input id="telePhone" type="text" name="telePhone" style="width:200px;" class="easyui-validatebox" required="true" >				 
					    <font color="red">*</font>
					</td>
				</tr> 
				<tr>
					<th align="right">部門：</th>
					<td>
					    <input id="orgId" type="hidden" name="orgId" />
						<input id="orgName" type="text" name="orgName" readonly="readonly" style="width:150px;float:left;margin-left:12px;" class="easyui-validatebox" required="true"/>
						<a id="chooseParent" href="#" class="easyui-linkbutton">選擇</a>
					    <font color="red">*</font>
					</td>
					<th align="right">廠區：</th>
					<td>
					    <input name="dominate" id="dominate" class="easyui-combobox" style="width:200px;"
						   data-options="
						   		valueField: 'label',
								textField: 'value',
								editable: false,
								data: [{
									label: '',
									value: '請選擇'
								},{
									label: 'ZZK',
									value: '鄭州綜保區'
								},{
									label: 'ZZC',
									value: '鄭州加工區'
								},{
									label: 'JYX',
									value: '濟源廠區'
								},{
									label: 'JCX',
									value: '晉城廠區'
								},{
									label: 'LHX',
									value: '龍華廠區'
								},{
									label: 'GLX',
									value: '觀瀾廠區'
								},{
									label: 'HBX',
									value: '鶴壁廠區'
								},{
									label: 'NH',
									value: '內湖廠區'
								}]"/>
					    <font color="red">*</font>
					</td>
				</tr> 
				<tr>
					<th align="right">Email：</th>
					<td>
					    <input id="email" type="text" name="email" style="width:200px;" class="easyui-validatebox" required="true" data-options="required:true,validType:'email'">
					    <font color="red">*</font>
					</td>
					<th align="right"></th>
					<td>
					</td>
				</tr>
				<tr>
					<th align="right">描述:</th>
					<td colspan="3">
						<textarea rows="4" style="width:95%;" id="description" name="description" validType="maxLength[150]"></textarea>									
					</td>
				</tr>			
			</table>	
			<br>			
			<div style="padding:5px;text-align:center;">
				<a id="submit" href="#" class="easyui-linkbutton" icon="icon-ok">保存</a>
				&nbsp;&nbsp;&nbsp;
				<a id="cancel" href="#" class="easyui-linkbutton" icon="icon-cancel">取消</a>
			</div>
		</form>
	</div>  
	
	<div id="optionWinParent" class="easyui-window" title="選擇部門" closed="true" closable="true" modal="true"  
	     collapsible="false" minimizable="false" maximizable="false" draggable="true" resizable="false"
	     style="width:1000px;height:450px;padding:5px;left:60px;top:50px;background: #fafafa;display:none" align="center">
	     	<div style="width: 100%;height: 100%;overflow:auto;">
				<table id="qryOrgParentGrid" toolbar="#tbOrgParent"></table>
				<div id="tbOrgParent">
					<form action="" id="tbFormOrgParentId" style="margin:0px;margin-bottom:10px;">
						<table width="100%" id="tableShowOrgParent">
							<tr>
								<td width="60%" style="white-space:nowrap;" align="left">
				                                        組織代碼：<input  name="f_orgCode" id="s_orgCode" size="8"  type="text"/>
				                                        組織名稱：<input  name="f_orgName" id="s_orgName" size="8"    type="text"/>
				          	        <input  name="f_enable" id="s_enable" size="8"    type="hidden"/>
									<a href="#" id="btnreset" class="easyui-linkbutton" iconCls="icon-sync" plain="true" onclick="resetParent()">重置</a>
									<a class="easyui-linkbutton" iconCls="icon-search" plain="true" href="#" onclick="queryOrgParentByParams()">查询</a>
								</td>
								<td width="40%" style="white-space:nowrap;padding-right:5px" align="right">
									<div style="padding:5px;text-align:center;">
										<a id="submitOrgParent" href="#" class="easyui-linkbutton" icon="icon-ok">確定</a>
										&nbsp;&nbsp;&nbsp;
										<a id="cancelOrgParent" href="#" class="easyui-linkbutton" icon="icon-cancel">取消</a>
									</div>
								</td>
							</tr>
						</table>
					</form>
				</div>
			</div>
	</div>
	
	<div id="optionWinModifyPwd" class="easyui-window" title="重置密碼" closed="true" modal="true" 
	     collapsible="false" minimizable="false" maximizable="false" draggable="true" resizable="false"
	     style="width:400px;height:200px;padding:5px;left:60px;top:50px;background: #fafafa;display:none" align="center">		
		<form id="pwdForm" name="pwdForm" action="" method="post">
			<input type="hidden" id="pwd_userId" name="userId"/>
        	<table class="formTbl" style="font-size:13px">
				<tr>
					<th align="right">新密碼：</th>
					<td>
					    <input id="pwd_password" type="password" name="password" style="width:90%" class="easyui-validatebox" required="true">
					    <font color="red">*</font>
					</td>
				</tr>
				<tr>
					<th align="right">確認密碼：</th>
					<td>
					    <input id="pwd_repassword" type="password" style="width:90%" class="easyui-validatebox"  required="true" >				 
					    <font color="red">*</font>
					</td>
				</tr>
			</table>	
			<br>			
			<div style="padding:5px;text-align:center;">
				<a id="submitPwd" href="#" class="easyui-linkbutton" icon="icon-ok">重置</a>
				&nbsp;&nbsp;&nbsp;
				<a id="cancelPwd" href="#" class="easyui-linkbutton" icon="icon-cancel">取消</a>
			</div>
		</form>
	</div> 
  </body>
</html>